﻿Public NotInheritable Class LineaPedido

    Protected _idFab As Byte = 0
    Protected _Linea As Byte = 0
    Protected _numPedido As Int32 = 0
    Protected _Modelo As Int16 = 0
    Protected _modificar As Boolean

    Public Event CargarListaPedios(ByVal Modificado As Boolean)

    Public Sub New(ByVal fab As Byte, ByVal num As Int32, ByVal linea As Byte, ByVal Modelo As Int16, Optional ByVal Modify As Boolean = False)
        Dim bndSrc As New BindingSource
        Dim sqlStr As String = New String("SELECT * FROM Extras WHERE idFabricante = " & _
                                          fab & " AND applyTo <> 0")
        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        _idFab = fab
        _Linea = linea
        _numPedido = num
        _modificar = Modify
        _Modelo = Modelo

        With CmbExtras
            .DataSource = Nothing
            .DataSource = DataAccess.GetRecords(sqlStr, CommandType.Text)
            .DisplayMember = "Descripcion"
            .ValueMember = "idExtra"
        End With

        If _modificar Then
            Label6.Text = "Modificar Referencia"

            sqlStr = "SELECT * FROM LineasPedido WHERE Numero=" & _numPedido & " AND Linea=" & _Linea
            bndSrc.DataSource = DataAccess.GetRecords(sqlStr, CommandType.Text)

            If bndSrc.Count > 0 Then
                For Each Objeto As Control In Me.Controls
                    If TypeOf Objeto Is TextBox And Objeto.Tag <> "!" Then
                        Objeto.DataBindings.Clear()
                        Objeto.DataBindings.Add(New Binding("Text", bndSrc, Objeto.Tag))
                        'AddHandler Objeto.TextChanged, AddressOf Fabricantes_TextChanged
                    End If
                Next

                CmbExtras.DataBindings.Clear()
                CmbExtras.DataBindings.Add("SelectedValue", bndSrc, "Extra")

            End If

        End If

    End Sub

    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        MyBase.Close()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Dim Valores(8) As String
        Dim Campos(8) As String
        Dim k As UInt16 = 3

        Campos.SetValue("@Numero", 0)
        Valores.SetValue(_numPedido.ToString, 0)

        Campos.SetValue("@Linea", 1)
        Valores.SetValue(_Linea.ToString, 1)

        Campos.SetValue("@Modelo", 2)
        Valores.SetValue(_Modelo.ToString, 2)

        For Each Objeto As Control In Me.Controls
            If TypeOf Objeto Is TextBox Then
                Campos.SetValue("@" + Objeto.Tag, k)
                Valores.SetValue(Trim(Objeto.Text), k)
                k += 1
            ElseIf TypeOf Objeto Is System.Windows.Forms.ComboBox Then
                Dim nuevoCombo As New ComboBox
                nuevoCombo = Objeto
                Campos.SetValue("@" + nuevoCombo.Tag, k)
                Try
                    Valores.SetValue(nuevoCombo.SelectedValue.ToString, k)
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                    Valores.SetValue("0", k)
                End Try
                k += 1
                nuevoCombo = Nothing
            End If
        Next
        Valores(4) = Valores(4).Replace(",", ".")
        If _modificar Then
            If DataAccess.InsertNewRecord(Campos, Valores, "updateLineaPedido") = -1 Then _
                MessageBox.Show("Error al guardar registro")
        Else
            If DataAccess.InsertNewRecord(Campos, Valores, "insertLineaPedido") = -1 Then _
                MessageBox.Show("Error al guardar registro")
        End If
        Me.Dispose() 'If _modificar Then 
        RaiseEvent CargarListaPedios(_modificar)
    End Sub

    Private Sub txtEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles TextBox2.KeyPress, TextBox4.KeyPress
        'Test for numeric value or backspace in first column
        If IsNumeric(e.KeyChar.ToString()) _
            Or e.KeyChar = ChrW(Keys.Back) Or e.KeyChar = "," Then
            e.Handled = False 'if numeric display
        Else
            e.Handled = True  'if non numeric don't display
        End If
    End Sub

    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
        BtnSave.Enabled = CBool(TextBox1.Text.Length) And CBool(TextBox2.Text.Length) And CBool(TextBox3.Text.Length) And CBool(TextBox4.Text.Length)
    End Sub

    Private Sub LineaPedido_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        For Each Kntrol As Control In Me.Controls
            If TypeOf Kntrol Is TextBox Then
                Kntrol.Text = Trim(Kntrol.Text)
            End If
        Next
    End Sub
End Class
